using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Siderys.Blog.CustomField
{
    public class BdcWss : SPFieldText
    {
        private string mConnectonString = string.Empty;
        private string mSelectString = string.Empty;

        private static Dictionary<int, string> mDicConnectionStriong = new Dictionary<int, string>();
        private static Dictionary<int, string> mDicSelectString = new Dictionary<int, string>();

        public BdcWss(SPFieldCollection pFields, string pFieldName)
            : base(pFields, pFieldName)
        {
            
        }

        public BdcWss(SPFieldCollection pFields, string pTypeName, string pDisplayName)
            : base(pFields, pTypeName, pDisplayName)
        {
            
        }

        public void Init()
        {
            mConnectonString = this.GetCustomProperty("StringConnection").ToString();
            mSelectString = this.GetCustomProperty("StringSelect").ToString();
        }

        private int GetContextId
        {
            get
            {
                return SPContext.Current.GetHashCode();
            }
        }
        public string ConeectionString
        {
            get
            {
                return mDicConnectionStriong.ContainsKey(GetContextId) ? mDicConnectionStriong[GetContextId] : mConnectonString;
            }
            set
            {
                mConnectonString = value;
            }
        }

        public string SelectString
        {
            get
            {
                return mDicSelectString.ContainsKey(GetContextId) ? mDicSelectString[GetContextId] : mSelectString;
            }
            set
            {
                mSelectString = value;
            }
        }

        public void UpdateConnectionStringProperty(string pValue)
        {
            mDicConnectionStriong[GetContextId] = pValue;
        }

        public void UpdateSelectStringProperty(string pValue)
        {
            mDicSelectString[GetContextId] = pValue;
        }

        public override void OnAdded(SPAddFieldOptions op)
        {
            base.OnAdded(op);
            Update();
        }

        public override void Update()
        {
            this.SetCustomProperty("StringConnection", ConeectionString);
            this.SetCustomProperty("StringSelect", SelectString);
            base.Update();
            if (mDicConnectionStriong.ContainsKey(GetContextId))
            {
                mDicConnectionStriong.Remove(GetContextId);
            }
            if (mDicSelectString.ContainsKey(GetContextId))
            {
                mDicSelectString.Remove(GetContextId);
            }
        }

        public override Microsoft.SharePoint.WebControls.BaseFieldControl FieldRenderingControl
        {
            get
            {
                Microsoft.SharePoint.WebControls.BaseFieldControl BdcWssControl = new BdcWssFieldType();
                BdcWssControl.FieldName = InternalName;
                return BdcWssControl;
            }
        }
    
    }
}
